import datetime
import re
from flask import Flask,Blueprint,request,jsonify
from database import *
from conf.comm import sqlalchemy_context
import json


#实例化蓝图
bp_lianxi = Blueprint('lianxi',__name__)



#删改查  cate
@bp_lianxi.route('/lianxi/',methods=['POST','GET','PUT','DELETE'])
@sqlalchemy_context()
def lianxi():
    if request.method=='POST':
        data = request.data
        if data:
            data = json.loads(request.data)
        else:
            data=request.form
        cate = Cate(name=data['name'])
        db.session.add(cate)
        db.session.commit()
        return jsonify({"code":200})


    elif request.method=='GET':

        page = int(request.args.get('page'))
        page_size = 3
        start = (page-1)*page_size
    
        cate = Cate.query.all()
        
        if page:
            cates = Cate.query.offset(start).limit(page_size)
            list = []
            for i in cates:
                dic={}
                dic['id']=i.id
                dic['name']=i.name
                list.append(dic)
            return jsonify({"code":200,'list':list})
        else:
            list = []
            for i in cate:
                dic={}
                dic['id']=i.id
                dic['name']=i.name
                list.append(dic)
            return jsonify({"code":200,'list':list})




    elif request.method=='PUT':
        id = request.args.get('id')
        name = request.args.get('name')
        if not id:
            return jsonify({"code":10011})
        #根据id删除
        Cate.query.filter_by(id=id).update({'name':name})
        db.session.commit()
        #返回结果
        return jsonify({'code':200})


    else :
        id = request.args.get('id')
        if not id:
            return jsonify({"code":10010})
        #根据id删除
        Cate.query.filter_by(id=id).delete()
        db.session.commit()
        #返回结果
        return jsonify({'code':200})

#展示分类
@bp_lianxi.route('/showcate/',methods=['GET'])
@sqlalchemy_context()
def showcate():
    list = []
    cate = Cate.query.all()
    for i in cate:
        dic={}
        dic['id']=i.id
        dic['name']=i.name
        list.append(dic)
    return jsonify({"code":200,'list':list})

import os
#添加图片
@bp_lianxi.route('/upload/',methods=['GET','POST'])
def upload():
    if request.method == 'POST':
        file = request.files['file']
        file.save(os.path.join('./static/upload/',file.filename))
        return jsonify({"code":200,'pic':file.filename})


#添加
@bp_lianxi.route('/keadd/',methods=['GET','POST'])
def keadd():
    if request.method == 'POST':
        #接收客户传递的参数
        data= request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        #构造数据
        
        course = Course(name=data['name'],description=data['description'],tname=data['tname'],pic=data['pic'],vedios=json.dumps(data['vedios'],ensure_ascii=False),cid=data['cid'])
        #添加
        db.session.add(course)
        #提交
        db.session.commit()
        #返回
        return jsonify({'code':200})

    elif request.method=='GET':
        print(request.args)
        #获取参数
        #当前点击的页面，如果没点击默认为1
        page = int(request.args.get('page'))
        #每页显示多少条
        size = int(request.args.get('size'))
        #读取数据
        start = (page-1)*size
        course = Course.query.offset(start).limit(size)
        #对结果解析
        list=[]
        if course:
            for i in course:
                list.append({'id':i.id,'name':i.name,'pic':i.pic,'tname':i.tname})
                
        #获取共有多少条记录
        total = Course.query.count()
        #返回
        return jsonify({"code":200,'list':list,'total':total})
        

@bp_lianxi.route('/courseM/',methods=['GET','POST'])
def courseM():
    if request.method == "GET":
        id = request.args.get('id')
        if id:
            course = Course.query.filter_by(id=id).first()
            cour = {'id':course.id,'name':course.name,'description':course.description,'tname':course.tname,'pic':course.pic,'vedios':course.vedios,'cid':course.cid}
            return jsonify({"code":200,'course':cour})
    